package com.ruoyi.project.system.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.service.ISysAttributeService;
import com.ruoyi.project.system.service.ISysTypeDetailService;
import com.ruoyi.project.system.service.ISysTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.service.ISysSkuService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 规格Controller
 *
 * @author jjp
 * @date 2020-04-20
 */
@RestController
@Api(description = "规格Controller", tags = "规格Controller")
@RequestMapping("/system/sku")
public class SysSkuController extends BaseController {
    @Autowired
    private ISysSkuService sysSkuService;

    @Autowired
    private ISysAttributeService sysAttributeService;

    @Autowired
    private ISysTypeDetailService sysTypeDetailService;
    @Autowired
    private ISysTypeService sysTypeService;

    /**
     * 查询规格列表
     */
    @PreAuthorize("@ss.hasPermi('system:sku:list')")
    @ApiOperation("查询规格列表")
    @GetMapping("/list")
    public TableDataInfo list(SysSku sysSku) {
        startPage();
        List<SysSku> list = sysSkuService.selectSysSkuList(sysSku);
        return getDataTable(list);
    }

    /**
     * 获取规格详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sku:query')")
    @ApiOperation("获取规格详细信息")
    @GetMapping(value = "/{skuId}")
    public AjaxResult getInfo(@PathVariable("skuId") Long skuId) {
        return AjaxResult.success(sysSkuService.selectSysSkuById(skuId));
    }

    /**
     * 新增规格
     */
    @PreAuthorize("@ss.hasPermi('system:sku:add')")
    @ApiOperation("新增规格")
    @Log(title = "规格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<SysSku> sku) {
        for (int i = 0; i < sku.size(); i++) {
            SysSku sysSku = sku.get(i);
            sysSkuService.insertSysSku(sysSku);
        }
        return AjaxResult.success();
    }

    /**
     * 修改规格
     */
    @PreAuthorize("@ss.hasPermi('system:sku:edit')")
    @ApiOperation("修改规格")
    @Log(title = "规格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody List<SysSku> sku) {
        Long typeDetailId = sku.get(0).getTypeDetailId();
        sysSkuService.deleteSysSkuByTypeDetailId(typeDetailId);
        for (int i = 0; i < sku.size(); i++) {
            SysSku sysSku = sku.get(i);
            if (sysSku.getData() == null) {
                return AjaxResult.success();
            }
            sysSkuService.insertSysSku(sysSku);
        }
        return AjaxResult.success();
    }

    /**
     * 删除规格
     */
    @PreAuthorize("@ss.hasPermi('system:sku:remove')")
    @ApiOperation("删除规格")
    @Log(title = "规格", businessType = BusinessType.DELETE)
    @DeleteMapping("/{skuIds}")
    public AjaxResult remove(@PathVariable Long[] skuIds) {
        return toAjax(sysSkuService.deleteSysSkuByIds(skuIds));
    }


    /**
     * 获取规格详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sku:query')")
    @ApiOperation("获取规格详细信息")
    @GetMapping(value = "/getInfoTypeDetailId/{typeId}/{typeDetailId}")
    public AjaxResult getInfoTypeDetailId(@PathVariable("typeId") Long typeId, @PathVariable("typeDetailId") Long typeDetailId) {
        if (typeId == null) {
            return AjaxResult.error();
        }
        List<SysAttribute> sysAttributes = sysAttributeService.selectSysAttributeByTypeId(typeId);
        SysTypeDetail sysTypeDetail = sysTypeDetailService.selectSysTypeDetailById(typeDetailId);
        SysSku sysSku = new SysSku();
        sysSku.setTypeDetailId(typeDetailId);
        List<SysSku> sysSkus = sysSkuService.selectSysSkuList(sysSku);
        Map<String, Object> map = new HashMap<>();
        map.put("sysAttribute", sysAttributes);
        map.put("sysSku", sysSkus);
        map.put("sysTypeDetail", sysTypeDetail);
        return AjaxResult.success(map);

    }


    /**
     * 修改规格
     */
    @PreAuthorize("@ss.hasPermi('system:sku:edit')")
    @ApiOperation("修改规格")
    @Log(title = "规格", businessType = BusinessType.UPDATE)
    @PutMapping("/allSku")
    public AjaxResult allSku(@RequestBody SysSkuSku sysSkuSku) {
        SysTypeDetail sysTypeDetail = sysSkuSku.getSysTypeDetail();
        List<SysSku> sku = sysSkuSku.getSysSku();
        Long typeId = sysTypeDetail.getTypeId();

        SysType sysType = new SysType();
        if (sysTypeDetail.getTypeDetailId() != null) {
            sysTypeDetailService.deleteSysTypeDetailById(sysTypeDetail.getTypeDetailId());
        }
        sysTypeDetailService.insertSysTypeDetail(sysTypeDetail);

        Long typeDetailId = sysTypeDetail.getTypeDetailId();
        sysType.setTypeId(typeId);
        sysType.setTypeDetailId(typeDetailId);
        sysTypeService.updateSysTypes(sysType);
        sysSkuService.deleteSysSkuByTypeDetailId(typeDetailId);
        for (int i = 0; i < sku.size(); i++) {
            SysSku sysSku = sku.get(i);
            if (sysSku.getData() == null) {
                return AjaxResult.success();
            }
            sysSku.setTypeDetailId(typeDetailId);
            sysSkuService.insertSysSku(sysSku);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("typeId", typeId);
        map.put("typeDetailId", typeDetailId);
        return AjaxResult.success(map);
    }


}
